<html>
<head><meta charset="utf-8"><title>Pass function to map · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html">Pass function to map</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240111727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240111727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240111727">(May 24 2021 at 21:03)</a>:</h4>
<p>Is it possible to pass a function to an iterator adapter (e.g. <code>map</code>)?<br>
For example, what am I doing wrong here?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">add_one</span><span class="p">(</span><span class="n">x</span>: <span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">add_ones</span><span class="p">(</span><span class="n">xs</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">u32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Works:</span>
<span class="w">    </span><span class="c1">// xs.iter().map(|x| x + 1).collect()</span>

<span class="w">    </span><span class="c1">// Will not compile:</span>
<span class="w">    </span><span class="n">xs</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">map</span><span class="p">(</span><span class="n">add_one</span><span class="p">).</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="240112104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240112104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240112104">(May 24 2021 at 21:07)</a>:</h4>
<p><code>iter()</code> will give you <code>&amp;u32</code>s while <code>add_one</code> accepts <code>u32</code>s.  it will work if you use <code>into_iter()</code> for example</p>



<a name="240112216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240112216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240112216">(May 24 2021 at 21:08)</a>:</h4>
<p>Another option would be adding a <code>.copied()</code> before the <code>.map(add_one)</code></p>



<a name="240113415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240113415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> miles <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240113415">(May 24 2021 at 21:18)</a>:</h4>
<p>Thanks. So now I'm pretty confused about this next example. Seems like <code>do_thing</code> should have the same signature as <code>|x| do_thing(x)</code>. Is there a way to ask the compiler what it thinks the type signatures should be (similar to typed-holes in haskell)?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">do_thing</span><span class="p">(</span><span class="n">s</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">do_things</span><span class="p">(</span><span class="n">words</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nb">Vec</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Works:</span>
<span class="w">    </span><span class="c1">// words.iter().map(|x| do_thing(x)).collect()</span>

<span class="w">    </span><span class="c1">// Will not compile</span>
<span class="w">    </span><span class="n">words</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">map</span><span class="p">(</span><span class="n">do_thing</span><span class="p">).</span><span class="n">collect</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I was originally going to cite <code>xs.iter().map(|x| add_one(x)).collect()</code> as a counter example, but this fails to compile (as expected based on your recent explanations).</p>



<a name="240118641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240118641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240118641">(May 24 2021 at 22:04)</a>:</h4>
<p>The best way I know to get the compiler to tell you the type of an expression is to put an <code>as ()</code> somewhere -- that puts the type right in the first line of the error.</p>
<p>The usual closure weirdness is with methods, because of autoderef.  Where <code>.map(|x| x.foo())</code> works (because it's <code>.map(|x| (*x).foo())</code> or something) but <code>.map(TheType::foo)</code> doesn't.</p>



<a name="240127232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240127232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240127232">(May 24 2021 at 23:43)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> I do <code>let () = </code> all the time to get the compiler to tell me types. Never thought about doing <code>as ()</code>; that's handy.</p>



<a name="240129678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240129678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240129678">(May 25 2021 at 00:21)</a>:</h4>
<p>It's an old trick from IRC -- the bot there only showed the first line of an error, and <code>let _: () = 4_i32</code> is just <code>error[E0308]: mismatched types</code>, but <code>4_i32 as ()</code> gives <code> error[E0605]: non-primitive cast: `i32` as `()` </code></p>



<a name="240130516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pass%20function%20to%20map/near/240130516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pass.20function.20to.20map.html#240130516">(May 25 2021 at 00:35)</a>:</h4>
<p>Of course, even shorter is just <code>4_i32()</code>: <code> error[E0618]: expected function, found `i32` </code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>